

<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
  <meta charset="utf-8" />
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  
  <title>error handling &mdash; Ceph Documentation</title>
  

  
  <link rel="stylesheet" href="../../../_static/ceph.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/graphviz.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/css/custom.css" type="text/css" />

  
  
    <link rel="shortcut icon" href="../../../_static/favicon.ico"/>
  

  
  

  

  
  <!--[if lt IE 9]>
    <script src="../../../_static/js/html5shiv.min.js"></script>
  <![endif]-->
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
        <script src="../../../_static/jquery.js"></script>
        <script src="../../../_static/underscore.js"></script>
        <script src="../../../_static/doctools.js"></script>
    
    <script type="text/javascript" src="../../../_static/js/theme.js"></script>

    
    <link rel="index" title="Index" href="../../../genindex/" />
    <link rel="search" title="Search" href="../../../search/" />
    <link rel="next" title="osd" href="../osd/" />
    <link rel="prev" title="crimson" href="../crimson/" /> 
</head>

<body class="wy-body-for-nav">

   
  <header class="top-bar">
    

















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../" class="icon icon-home"></a> &raquo;</li>
        
          <li><a href="../../internals/">Ceph 内幕</a> &raquo;</li>
        
          <li><a href="../">Crimson developer documentation</a> &raquo;</li>
        
      <li>error handling</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
          
            <a href="../../../_sources/dev/crimson/error-handling.rst.txt" rel="nofollow"> View page source</a>
          
        
      </li>
    
  </ul>

  
  <hr/>
</div>
  </header>
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search"  style="background: #eee" >
          

          
            <a href="../../../">
          

          
            
            <img src="../../../_static/logo.png" class="logo" alt="Logo"/>
          
          </a>

          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../search/" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        
        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../../../start/intro/">Ceph 简介</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../install/">安装 Ceph</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../cephadm/">Cephadm</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../rados/">Ceph 存储集群</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../cephfs/">Ceph 文件系统</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../rbd/">Ceph 块设备</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../radosgw/">Ceph 对象网关</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../mgr/">Ceph 管理器守护进程</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../mgr/dashboard/">Ceph 仪表盘</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../api/">API 文档</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../architecture/">体系结构</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../developer_guide/">开发者指南</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="../../internals/">Ceph 内幕</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="../../blkin/">Tracing Ceph With LTTng</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../blkin/#tracing-ceph-with-blkin">Tracing Ceph With Blkin</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../bluestore/">BlueStore Internals</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../cache-pool/">Cache pool</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../ceph_krb_auth/">如何配置好 Ceph Kerberos 认证的详细文档</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../cephfs-mirroring/">CephFS Mirroring</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../cephfs-reclaim/">CephFS Reclaim Interface</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../cephfs-snapshots/">CephFS 快照</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../cephx/">Cephx</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../cephx_protocol/">Cephx 认证协议详细阐述</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../config/">配置管理系统</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../config-key/">config-key layout</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../context/">CephContext</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../continuous-integration/">Continuous Integration Architecture</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../corpus/">资料库结构</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../cpu-profiler/">Oprofile 的安装</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../cxx/">C++17 and libstdc++ ABI</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../deduplication/">去重</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../delayed-delete/">CephFS delayed deletion</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../dev_cluster_deployement/">开发集群的部署</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../dev_cluster_deployement/#id5">在同一机器上部署多套开发集群</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../development-workflow/">开发流程</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../documenting/">为 Ceph 写作文档</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../encoding/">序列化（编码、解码）</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../erasure-coded-pool/">纠删码存储池</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../file-striping/">File striping</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../freebsd/">FreeBSD Implementation details</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../generatedocs/">Ceph 文档的构建</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../health-reports/">Health Reports</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../iana/">IANA 号</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../kubernetes/">Hacking on Ceph in Kubernetes with Rook</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../libs/">库体系结构</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../logging/">集群日志的用法</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../logs/">调试日志</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../macos/">在 MacOS 上构建</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../messenger/">Messenger notes</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../mon-bootstrap/">Monitor bootstrap</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../mon-elections/">Monitor Elections</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../mon-on-disk-formats/">ON-DISK FORMAT</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../mon-osdmap-prune/">FULL OSDMAP VERSION PRUNING</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../msgr2/">msgr2 协议（ msgr2.0 和 msgr2.1 ）</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../network-encoding/">Network Encoding</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../network-protocol/">网络协议</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../object-store/">对象存储架构概述</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../osd-class-path/">OSD class path issues</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../peering/">互联</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../perf/">Using perf</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../perf_counters/">性能计数器</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../perf_histograms/">Perf histograms</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../placement-group/">PG （归置组）说明</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../quick_guide/">开发者指南（快速）</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../rados-client-protocol/">RADOS 客户端协议</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../rbd-diff/">RBD 增量备份</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../rbd-export/">RBD Export &amp; Import</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../rbd-layering/">RBD Layering</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../release-checklists/">Release checklists</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../release-process/">Ceph Release Process</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../seastore/">SeaStore</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../sepia/">Sepia 社区测试实验室</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../session_authentication/">Session Authentication for the Cephx Protocol</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../testing/">测试笔记</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../versions/">Public OSD Version</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../vstart-ganesha/">NFS CephFS-RGW Developer Guide</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../wireshark/">Wireshark Dissector</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../zoned-storage/">Zoned Storage Support</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../osd_internals/">OSD 开发者文档</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../mds_internals/">MDS 开发者文档</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../radosgw/">RADOS 网关开发者文档</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../ceph-volume/">ceph-volume 开发者文档</a></li>
<li class="toctree-l2 current"><a class="reference internal" href="../">Crimson developer documentation</a><ul class="current">
<li class="toctree-l3"><a class="reference internal" href="../crimson/">crimson</a></li>
<li class="toctree-l3 current"><a class="current reference internal" href="#">error handling</a></li>
<li class="toctree-l3"><a class="reference internal" href="../osd/">osd</a></li>
<li class="toctree-l3"><a class="reference internal" href="../pipeline/">The <code class="docutils literal notranslate"><span class="pre">ClientRequest</span></code> pipeline</a></li>
<li class="toctree-l3"><a class="reference internal" href="../poseidonstore/">PoseidonStore</a></li>
</ul>
</li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../../governance/">项目管理</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../foundation/">Ceph 基金会</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../ceph-volume/">ceph-volume</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../releases/general/">Ceph 版本（总目录）</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../releases/">Ceph 版本（索引）</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../security/">Security</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../glossary/">Ceph 术语</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../jaegertracing/">Tracing</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../translation_cn/">中文版翻译资源</a></li>
</ul>

            
          
        </div>
        
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../">Ceph</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
<div id="dev-warning" class="admonition note">
  <p class="first admonition-title">Notice</p>
  <p class="last">This document is for a development version of Ceph.</p>
</div>
  <div id="docubetter" align="right" style="padding: 5px; font-weight: bold;">
    <a href="https://pad.ceph.com/p/Report_Documentation_Bugs">Report a Documentation Bug</a>
  </div>

  
  <div class="section" id="error-handling">
<h1>error handling<a class="headerlink" href="#error-handling" title="Permalink to this headline">¶</a></h1>
<p>In Seastar, a <code class="docutils literal notranslate"><span class="pre">future</span></code> represents a value not yet available but that can become
available later. <code class="docutils literal notranslate"><span class="pre">future</span></code> can have one of following states:</p>
<ul class="simple">
<li><p>unavailable: value is not available yet,</p></li>
<li><p>value,</p></li>
<li><p>failed: an exception was thrown when computing the value. This exception has
been captured and stored in the <code class="docutils literal notranslate"><span class="pre">future</span></code> instance via <code class="docutils literal notranslate"><span class="pre">std::exception_ptr</span></code>.</p></li>
</ul>
<p>In the last case, the exception can be processed using <code class="docutils literal notranslate"><span class="pre">future::handle_exception()</span></code> or
<code class="docutils literal notranslate"><span class="pre">future::handle_exception_type()</span></code>. Seastar even provides <code class="docutils literal notranslate"><span class="pre">future::or_terminate()</span></code> to
terminate the program if the future fails.</p>
<p>But in Crimson, quite a few errors are not serious enough to fail the program entirely.
For instance, if we try to look up an object by its object id, and that operation could
fail because the object does not exist or it is corrupted, we need to recover that object
for fulfilling the request instead of terminating the process.</p>
<p>In other words, these errors are expected. Moreover, the performance of the unhappy path
should also be on par with that of the happy path. Also, we want to have a way to ensure
that all expected errors are handled. It should be something like the statical analysis
performed by compiler to spit a warning if any enum value is not handled in a <code class="docutils literal notranslate"><span class="pre">switch-case</span></code>
statement.</p>
<p>Unfortunately, <code class="docutils literal notranslate"><span class="pre">seastar::future</span></code> is not able to satisfy these two requirements.</p>
<ul class="simple">
<li><p>Seastar imposes re-throwing an exception to dispatch between different types of
exceptions. This is not very performant nor even scalable as locking in the language’s
runtime can occur.</p></li>
<li><p>Seastar does not encode the expected exception type in the type of the returned
<code class="docutils literal notranslate"><span class="pre">seastar::future</span></code>. Only the type of the value is encoded. This imposes huge
mental load on programmers as ensuring that all intended errors are indeed handled
requires manual code audit.</p></li>
</ul>
<p>So, “errorator” is created. It is a wrapper around the vanilla <code class="docutils literal notranslate"><span class="pre">seastar::future</span></code>.
It addresses the performance and scalability issues while embedding the information
about all expected types-of-errors to the type-of-future.:</p>
<div class="highlight-c++ notranslate"><div class="highlight"><pre><span></span><span class="k">using</span><span class="w"> </span><span class="n">ertr</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">crimson</span><span class="o">::</span><span class="n">errorator</span><span class="o">&lt;</span><span class="n">crimson</span><span class="o">::</span><span class="n">ct_error</span><span class="o">::</span><span class="n">enoent</span><span class="p">,</span><span class="w"></span>
<span class="w">                                </span><span class="n">crimson</span><span class="o">::</span><span class="n">ct_error</span><span class="o">::</span><span class="n">einval</span><span class="o">&gt;</span><span class="p">;</span><span class="w"></span>
</pre></div>
</div>
<p>In above example we defined an errorator that allows for two error types:</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">crimson::ct_error::enoent</span></code> and</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">crimson::ct_error::einval</span></code>.</p></li>
</ul>
<p>These (and other ones in the <code class="docutils literal notranslate"><span class="pre">crimson::ct_error</span></code> namespace) are basically
unthrowable wrappers over <code class="docutils literal notranslate"><span class="pre">std::error_code</span></code> to exclude accidental throwing
and ensure signaling errors in a way that enables compile-time checking.</p>
<p>The most fundamental thing in an errorator is a descendant of <code class="docutils literal notranslate"><span class="pre">seastar::future</span></code>
which can be used as e.g. function’s return type:</p>
<div class="highlight-c++ notranslate"><div class="highlight"><pre><span></span><span class="k">static</span><span class="w"> </span><span class="n">ertr</span><span class="o">::</span><span class="n">future</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span><span class="w"> </span><span class="n">foo</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">bar</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">  </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">bar</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">42</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">return</span><span class="w"> </span><span class="n">crimson</span><span class="o">::</span><span class="n">ct_error</span><span class="o">::</span><span class="n">einval</span><span class="o">::</span><span class="n">make</span><span class="p">();</span><span class="w"></span>
<span class="w">  </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">return</span><span class="w"> </span><span class="n">ertr</span><span class="o">::</span><span class="n">make_ready_future</span><span class="p">(</span><span class="n">bar</span><span class="p">);</span><span class="w"></span>
<span class="w">  </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>
</div>
<p>It’s worth to note that returning an error that is not a part the errorator’s error set
would result in a compile-time error:</p>
<div class="highlight-c++ notranslate"><div class="highlight"><pre><span></span><span class="k">static</span><span class="w"> </span><span class="n">ertr</span><span class="o">::</span><span class="n">future</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span><span class="w"> </span><span class="n">foo</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">bar</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">  </span><span class="c1">// Oops, input_output_error is not allowed in `ertr`.  static_assert() will</span>
<span class="w">  </span><span class="c1">// terminate the compilation. This behaviour is absolutely fundamental for</span>
<span class="w">  </span><span class="c1">// callers -- to figure out about all possible errors they need to worry</span>
<span class="w">  </span><span class="c1">// about is enough to just take a look on the function&#39;s signature; reading</span>
<span class="w">  </span><span class="c1">// through its implementation is not necessary anymore!</span>
<span class="w">  </span><span class="k">return</span><span class="w"> </span><span class="n">crimson</span><span class="o">::</span><span class="n">ct_error</span><span class="o">::</span><span class="n">input_output_error</span><span class="o">::</span><span class="n">make</span><span class="p">();</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>
</div>
<p>The errorator concept goes further. It not only provides callers with the information
about all potential errors embedded in the function’s type; it also ensures at the caller
site that all these errors are handled. As the reader probably know, the main method
in <code class="docutils literal notranslate"><span class="pre">seastar::future</span></code> is <code class="docutils literal notranslate"><span class="pre">then()</span></code>. On errorated future it is available but only if errorator’s
error set is empty (literally: <code class="docutils literal notranslate"><span class="pre">errorator&lt;&gt;::future</span></code>); otherwise callers have
to use <code class="docutils literal notranslate"><span class="pre">safe_then()</span></code> instead:</p>
<div class="highlight-c++ notranslate"><div class="highlight"><pre><span></span><span class="n">seastar</span><span class="o">::</span><span class="n">future</span><span class="o">&lt;&gt;</span><span class="w"> </span><span class="n">baz</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">  </span><span class="k">return</span><span class="w"> </span><span class="n">foo</span><span class="p">(</span><span class="mi">42</span><span class="p">).</span><span class="n">safe_then</span><span class="p">(</span><span class="w"></span>
<span class="w">    </span><span class="p">[]</span><span class="w"> </span><span class="p">(</span><span class="k">const</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">bar</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">      </span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="s">&quot;the optimistic path! got bar=&quot;</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="n">bar</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="w"></span>
<span class="w">      </span><span class="k">return</span><span class="w"> </span><span class="n">ertr</span><span class="o">::</span><span class="n">now</span><span class="p">();</span><span class="w"></span>
<span class="w">    </span><span class="p">},</span><span class="w"></span>
<span class="w">    </span><span class="n">ertr</span><span class="o">::</span><span class="n">all_same_way</span><span class="p">(</span><span class="k">const</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">error_code</span><span class="o">&amp;</span><span class="w"> </span><span class="n">err</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">      </span><span class="c1">// handling errors removes them from errorator&#39;s error set</span>
<span class="w">      </span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="s">&quot;the error path! got err=&quot;</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="n">err</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span><span class="w"></span>
<span class="w">      </span><span class="k">return</span><span class="w"> </span><span class="n">ertr</span><span class="o">::</span><span class="n">now</span><span class="p">();</span><span class="w"></span>
<span class="w">    </span><span class="p">}).</span><span class="n">then</span><span class="p">([]</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">      </span><span class="c1">// as all errors have been handled, errorator&#39;s error set became</span>
<span class="w">      </span><span class="c1">// empty and the future instance returned from `safe_then()` has</span>
<span class="w">      </span><span class="c1">// `then()` available!</span>
<span class="w">      </span><span class="k">return</span><span class="w"> </span><span class="n">seastar</span><span class="o">::</span><span class="n">now</span><span class="p">();</span><span class="w"></span>
<span class="w">    </span><span class="p">});</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>
</div>
<p>In the above example <code class="docutils literal notranslate"><span class="pre">ertr::all_same_way</span></code> has been used to handle all errors in the same
manner. This is not obligatory – a caller can handle each of them separately. Moreover,
it can provide a handler for only a subset of errors. The price for that is the availability
of <code class="docutils literal notranslate"><span class="pre">then()</span></code>:</p>
<div class="highlight-c++ notranslate"><div class="highlight"><pre><span></span><span class="k">using</span><span class="w"> </span><span class="n">einval_ertr</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">crimson</span><span class="o">::</span><span class="n">errorator</span><span class="o">&lt;</span><span class="n">crimson</span><span class="o">::</span><span class="n">ct_error</span><span class="o">::</span><span class="n">einval</span><span class="o">&gt;</span><span class="p">;</span><span class="w"></span>

<span class="c1">// we can&#39;t return seastar::future&lt;&gt; (aka errorator&lt;&gt;::future&lt;&gt;) as handling</span>
<span class="c1">// as this level deals only with enoent leaving einval without a handler.</span>
<span class="c1">// handling it becomes a responsibility of a caller of `baz()`.</span>
<span class="n">einval_ertr</span><span class="o">::</span><span class="n">future</span><span class="o">&lt;&gt;</span><span class="w"> </span><span class="n">baz</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">  </span><span class="k">return</span><span class="w"> </span><span class="n">foo</span><span class="p">(</span><span class="mi">42</span><span class="p">).</span><span class="n">safe_then</span><span class="p">(</span><span class="w"></span>
<span class="w">    </span><span class="p">[]</span><span class="w"> </span><span class="p">(</span><span class="k">const</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">bar</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">      </span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="s">&quot;the optimistic path! got bar=&quot;</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="n">bar</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="w"></span>
<span class="w">      </span><span class="k">return</span><span class="w"> </span><span class="n">ertr</span><span class="o">::</span><span class="n">now</span><span class="p">();</span><span class="w"></span>
<span class="w">    </span><span class="p">},</span><span class="w"></span>
<span class="w">    </span><span class="c1">// provide a handler only for crimson::ct_error::enoent.</span>
<span class="w">    </span><span class="c1">// crimson::ct_error::einval stays unhandled!</span>
<span class="w">    </span><span class="n">crimson</span><span class="o">::</span><span class="n">ct_error</span><span class="o">::</span><span class="n">enoent</span><span class="o">::</span><span class="n">handle</span><span class="p">([]</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">      </span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="s">&quot;the enoent error path!&quot;</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span><span class="w"></span>
<span class="w">      </span><span class="k">return</span><span class="w"> </span><span class="n">ertr</span><span class="o">::</span><span class="n">now</span><span class="p">();</span><span class="w"></span>
<span class="w">    </span><span class="p">}));</span><span class="w"></span>
<span class="w">  </span><span class="c1">// .safe_then() above returned `errorator&lt;crimson::ct_error::einval&gt;::future&lt;&gt;`</span>
<span class="w">  </span><span class="c1">// which lacks `then()`.</span>
<span class="p">}</span><span class="w"></span>
</pre></div>
</div>
<p>That is, handling errors removes them from errorated future’s error set. This works
in the opposite direction too – returning new errors in <code class="docutils literal notranslate"><span class="pre">safe_then()</span></code> appends them
the error set. Of course, this set must be compliant with error set in the <code class="docutils literal notranslate"><span class="pre">baz()</span></code>’s
signature:</p>
<div class="highlight-c++ notranslate"><div class="highlight"><pre><span></span><span class="k">using</span><span class="w"> </span><span class="n">broader_ertr</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">crimson</span><span class="o">::</span><span class="n">errorator</span><span class="o">&lt;</span><span class="n">crimson</span><span class="o">::</span><span class="n">ct_error</span><span class="o">::</span><span class="n">enoent</span><span class="p">,</span><span class="w"></span>
<span class="w">                                        </span><span class="n">crimson</span><span class="o">::</span><span class="n">ct_error</span><span class="o">::</span><span class="n">einval</span><span class="p">,</span><span class="w"></span>
<span class="w">                                        </span><span class="n">crimson</span><span class="o">::</span><span class="n">ct_error</span><span class="o">::</span><span class="n">input_output_error</span><span class="o">&gt;</span><span class="p">;</span><span class="w"></span>

<span class="n">broader_ertr</span><span class="o">::</span><span class="n">future</span><span class="o">&lt;&gt;</span><span class="w"> </span><span class="n">baz</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">  </span><span class="k">return</span><span class="w"> </span><span class="n">foo</span><span class="p">(</span><span class="mi">42</span><span class="p">).</span><span class="n">safe_then</span><span class="p">(</span><span class="w"></span>
<span class="w">    </span><span class="p">[]</span><span class="w"> </span><span class="p">(</span><span class="k">const</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">bar</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">      </span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="s">&quot;oops, the optimistic path generates a new error!&quot;</span><span class="p">;</span><span class="w"></span>
<span class="w">      </span><span class="k">return</span><span class="w"> </span><span class="n">crimson</span><span class="o">::</span><span class="n">ct_error</span><span class="o">::</span><span class="n">input_output_error</span><span class="o">::</span><span class="n">make</span><span class="p">();</span><span class="w"></span>
<span class="w">    </span><span class="p">},</span><span class="w"></span>
<span class="w">    </span><span class="c1">// we have a special handler to delegate the handling up. For conveience,</span>
<span class="w">    </span><span class="c1">// the same behaviour is available as single argument-taking variant of</span>
<span class="w">    </span><span class="c1">// `safe_then()`.</span>
<span class="w">    </span><span class="n">ertr</span><span class="o">::</span><span class="n">pass_further</span><span class="p">{});</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>
</div>
<p>As it can be seen, handling and signaling errors in <code class="docutils literal notranslate"><span class="pre">safe_then()</span></code> is basically
an operation on the error set checked at compile-time.</p>
<p>More details can be found in <a class="reference external" href="https://www.slideshare.net/ScyllaDB/cepherrorator-throwcatchfree-compile-timechecked-exceptions-for-seastarfuture">the slides from ceph::errorator&lt;&gt; throw/catch-free,
compile time-checked exceptions for seastar::future&lt;&gt;</a>
presented at the Seastar Summit 2019.</p>
</div>



           </div>
           
          </div>
          <footer>
    <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
        <a href="../osd/" class="btn btn-neutral float-right" title="osd" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
        <a href="../crimson/" class="btn btn-neutral float-left" title="crimson" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
    </div>

  <hr/>

  <div role="contentinfo">
    <p>
        &#169; Copyright 2016, Ceph authors and contributors. Licensed under Creative Commons Attribution Share Alike 3.0 (CC-BY-SA-3.0).

    </p>
  </div> 

</footer>
        </div>
      </div>

    </section>

  </div>
  

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>